1557B - Moamen and k-subarrays - CodeForces Solution


greedy sortings *1100

Please click on ads to support us..

Python Code:

import sys
import math
import itertools

mod = 998244353
def ri(): return int(input())
def rl(): return list(map(int, input().split()))
def rls(): return list(map(str, input().split()))
def rm(): return map(int, input().split())
def isPowerOfTwo(x): return (x and (not(x & (x - 1))))
def decimalToBinary(n): return bin(n).replace("0b", "")


def nextPowerOf2(n):
    n -= 1
    n |= n >> 1
    n |= n >> 2
    n |= n >> 4
    n |= n >> 8
    n |= n >> 16
    n += 1
    return n


def highestPowerof2(x):
    x |= x >> 1
    x |= x >> 2
    x |= x >> 4
    x |= x >> 8
    x |= x >> 16
    return x ^ (x >> 1)


for _ in range(ri()):
    n, k = rm()
    a = rl()
    c = 0
    b = sorted(a)
    d = {}
    for i in range(n):
        d[a[i]] = i
            l = []
    for i in b:
        l.append((i, d[i]))
        d.pop(i)
        c = 0
    for i in range(1, n):
        if l[i][1]-l[i-1][1] != 1:
            c += 1
    if c < k:
        print("YES")
    else:
        print("NO")

C++ Code:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vb = vector<bool>;
using vvb = vector<vb>;
using vi = vector<ll>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vc = vector<char>;
using vvc = vector<vc>;
using vs = vector<string>;
#define pb push_back
#include <bits/stdc++.h>
using namespace std;
// ll mod = 1e9 + 7 ;
// const ll maxn = 1e5+1 ;
// ll fac[maxn],invfact[maxn];
// ll power(ll x, ll y) 
// { 
//     ll res = 1; 
//     x = x % mod; 
  
//     while (y > 0) { 
//         if (y & 1) 
//             res = (res * x) % mod; 
//         y = y >> 1; 
//         x = (x * x) % mod; 
//     } 
 
//     return res; 
// } 
// ll modInverse(ll n) 
// { 
//     return power(n, mod - 2); 
// } 
// ll NCR(ll n, ll r, ll p=mod) 
// {
//     if(r < 0 || n < 0)
//         assert(false);
 
//     if( n < r ) return 0;
 
//     if (r == 0 || r == n) 
//         return 1; 
    
//     return ( fac[n] * invfact[r]  % mod) * invfact[n - r] % mod;  
// }
// void comp()
// {
//     fac[0] = 1, invfact[0] = 1; 
    
//     for (ll i = 1; i < maxn; i++){ 
//         fac[i] = (fac[i - 1] * i) % mod;
//         invfact[i] = modInverse(fac[i]);
//     } 
// }
void solve(){
    ll n , k ;cin>>n>>k ;
    vector<pair<ll,ll>> num(n);
    for (ll i = 0; i < n; i++)
    {
        ll a ;cin>>a ;
        num[i].first = a , num[i].second = i ;
    }
    sort(num.begin(),num.end()) ;
    vector<ll>after(n-1) ;ll req = 1 ;
    for (ll i = 0; i < n-1; i++)
    {
        if (num[i+1].second!=num[i].second+1)
        {
            req++ ;
        }
    }
    // cout<<req<<endl ;
    if(req>k)cout<<"No\n";
    else cout<<"Yes\n";
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    // comp() ; -> to use ncr 
    ll t ;cin>>t ;
    while (t--)
    {
        solve();
    }
    
    return 0 ;
}


Comments

Submit
0 Comments
More Questions

1529A - Eshag Loves Big Arrays
19. Remove Nth Node From End of List
925. Long Pressed Name
1051. Height Checker
695. Max Area of Island
402. Remove K Digits
97. Interleaving String
543. Diameter of Binary Tree
124. Binary Tree Maximum Path Sum
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
501A - Contest
160A- Twins
752. Open the Lock
1535A - Fair Playoff
1538F - Interesting Function
1920. Build Array from Permutation
494. Target Sum
797. All Paths From Source to Target
1547B - Alphabetical Strings
1550A - Find The Array
118B - Present from Lena
27A - Next Test
785. Is Graph Bipartite
90. Subsets II
1560A - Dislike of Threes
36. Valid Sudoku
557. Reverse Words in a String III
566. Reshape the Matrix
167. Two Sum II - Input array is sorted
387. First Unique Character in a String